<?php

namespace app\controller\api\controller;

use app\popo\dto\OrdersCancelDTO;
use app\popo\dto\OrdersConfirmDTO;
use app\popo\dto\OrdersPageQueryDTO;
use app\popo\dto\OrdersRejectionDTO;
use app\service\OrderService;
use app\support\ResponseHelper;
use DI\Attribute\Inject;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\PutMapping;
use Shayvmo\WebmanAnnotations\Annotations\RequestMapping;
use Shayvmo\WebmanAnnotations\Annotations\RestController;
use Webman\Http\Request;

/**
 * 订单管理
 */
#[RestController("/api/order")]
class OrderController
{
    #[Inject]
    protected OrderService $orderServices;

    /**
     * 订单搜索
     */
    #[GetMapping("/conditionSearch")]
    public function conditionSearch(Request $request)
    {
        // 构建查询DTO
        $ordersPageQueryDTO = new OrdersPageQueryDTO();
        $ordersPageQueryDTO->page = (int)$request->get('page', 1);
        $ordersPageQueryDTO->pageSize = (int)$request->get('pageSize', 10);
        $ordersPageQueryDTO->number = $request->get('number');
        $ordersPageQueryDTO->phone = $request->get('phone');
        $ordersPageQueryDTO->status = $request->get('status') ? (int)$request->get('status') : null;
        $ordersPageQueryDTO->beginTime = $request->get('beginTime');
        $ordersPageQueryDTO->endTime = $request->get('endTime');
        
        $pageResult = $this->orderServices->conditionSearchAdmin($ordersPageQueryDTO);
        return ResponseHelper::successJson($pageResult->toArray());
    }

    /**
     * 各个状态的订单数量统计
     */
    #[GetMapping("/statistics")]
    public function statistics()
    {
        $orderStatisticsVO = $this->orderServices->statistics();
        return ResponseHelper::successJson($orderStatisticsVO);
    }

    /**
     * 订单详情
     */
    #[GetMapping("/details/{id}")]
    public function details(int $id)
    {
        $orderVO = $this->orderServices->details($id);
        return ResponseHelper::successJson($orderVO);
    }

    /**
     * 接单
     */
    #[PutMapping("/confirm")]
    public function confirm(Request $request)
    {
        $data = json_decode($request->rawBody(), true);
        $ordersConfirmDTO = new OrdersConfirmDTO();
        $ordersConfirmDTO->id = $data['id'];
        $ordersConfirmDTO->status = $data['status'] ?? 3; // 默认状态为已接单
        
        $this->orderServices->confirm($ordersConfirmDTO);
        return ResponseHelper::successJson();
    }

    /**
     * 拒单
     */
    #[PutMapping("/rejection")]
    public function rejection(Request $request)
    {
        $data = json_decode($request->rawBody(), true);
        $ordersRejectionDTO = new OrdersRejectionDTO();
        $ordersRejectionDTO->id = $data['id'];
        $ordersRejectionDTO->rejectionReason = $data['rejectionReason'];
        
        $this->orderServices->rejection($ordersRejectionDTO);
        return ResponseHelper::successJson();
    }

    /**
     * 取消订单
     */
    #[PutMapping("/cancel")]
    public function cancel(Request $request)
    {
        $data = json_decode($request->rawBody(), true);
        $ordersCancelDTO = new OrdersCancelDTO();
        $ordersCancelDTO->id = $data['id'];
        $ordersCancelDTO->cancelReason = $data['cancelReason'];
        
        $this->orderServices->cancel($ordersCancelDTO);
        return ResponseHelper::successJson();
    }

    /**
     * 派送订单
     */
    #[PutMapping("/delivery/{id}")]
    public function delivery(int $id)
    {
        $this->orderServices->delivery($id);
        return ResponseHelper::successJson();
    }

    /**
     * 完成订单
     */
    #[PutMapping("/complete/{id}")]
    public function complete(int $id)
    {
        $this->orderServices->complete($id);
        return ResponseHelper::successJson();
    }
}